﻿<script>
//nkGestures Mouse Gesture for Chrome
//Copyright (C) 2009  CJvoid, Niklen, BugVuln
//
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

//toolstrip by nk
//nkToolStrip object 处理每个网页发送来的消息包括config.html的消息
var nkToolStrip = {
	gesturesName : "GesturesArray",

	open_drag_name : "isOpenDrag",

	opr_name : "isOpenToRight",

	oif_name : "isOpenInFront",

	searchWebName : "SearchEngine",

	searchWeb : "http://www.google.cn/search?q=",
	
	open_hint_name : "isOpenHint",
	
	is_en_name : "isEnglish",
	
	active_direction_name : "ActiveDirection",
	
	homePage_name : "HomePage",
	
	activeDirection :'',
	
	homePage : '',

	isOpenToRight : false,

	isOpenDrag : false,

	isOpenInFront : false,
	
	isOpenHint : false,
	
	is_English : false,

	actionsConfig : new Array(
	"DU",
	"DR",
	"L",
	"R",
	"D",
	"UD",
	"",
	"UL",
	"UR",
	"LU",
	"RU",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
	"",
	"URDLD"
	),

	string2array : function(string,array)
	{
		var i,j;
		j = 0;
		for(i=0;i<array.length;i++)
		{
			var temp = '';
			while (j<string.length && string[j]!=',') {
				temp += string[j];
				j++;
			}
			j++;
			array[i] = temp;
		}
	},

	init : function () {

		if ( localStorage[this.open_drag_name] == 'enable' ) {
			this.isOpenDrag = true;
		}
		else this.isOpenDrag = false;
		
		if ( localStorage[this.open_hint_name] == 'enable' ) {
			this.isOpenHint = true;
		}
		else this.isOpenHint = false;
		
		if ( localStorage[this.is_en_name] == 'enable' ) {
			this.isEnglish = true;
		}
		else this.isEnglish = false;
		
		if ( localStorage[this.opr_name] == 'enable' ) {
			this.isOpenToRight = true;
		}
		else this.isOpenToRight = false;
		
		if ( localStorage[this.oif_name] == 'enable' ) {
			this.isOpenInFront = true;
		}
		else this.isOpenInFront = false;
		
		if (localStorage[this.gesturesName]) {
			this.actionsConfig = localStorage[this.gesturesName];
		}else{
			localStorage[this.gesturesName] = this.actionsConfig;
		}
		
		if (localStorage[this.searchWebName]) {
			this.searchWeb = localStorage[this.searchWebName];
		}else{
			localStorage[this.searchWebName] = this.searchWeb;
		}
		
		if (localStorage[this.homePage_name]) {
			this.homePage = localStorage[this.homePage_name];
		}else{
			localStorage[this.homePage_name] = this.homePage;
		}
		
		if (localStorage[this.active_direction_name]) {
			this.activeDirection = localStorage[this.active_direction_name];
		}else{
			localStorage[this.active_direction_name] = this.activeDirection;
		}	
	}
	
};

nkToolStrip.init();

chrome.extension.onConnect.addListener(
	function(port)
	{
		if(port.name != "nkGestures")
		return;
		port.onMessage.addListener(
			function(message)
			{
				var newTab;
				var link = '';
				switch(message[0])
				{
					case "do:dragtxt":
						if (message[1].indexOf("http://") < 0)
							link += nkToolStrip.searchWeb;
					case "do:dragurl":
						link += message[1];
						newTab = {'windowId':port.tab.windowId, 'url':link, 'selected':false};
						if(nkToolStrip.isOpenToRight)
							newTab.index = port.tab.index+1;
						if(nkToolStrip.activeDirection.indexOf(message[2])!=-1 || nkToolStrip.isOpenInFront) //支持多个方向拖拽设置
							newTab.selected = true;
						chrome.tabs.create(newTab);
						break;
					default:
						break;
				}

				switch(message)
				{
						case "do:newtab":
							newTab = {'windowId':port.tab.windowId, 'url':'chrome://newtab/', 'selected':true};
							if(nkToolStrip.isOpenToRight)
								newTab.index = port.tab.index + 1;
							chrome.tabs.create(newTab);
							break;
						case "do:openindex":
							chrome.tabs.update(port.tab.id,{'url':nkToolStrip.homePage, 'selected':true});
							break;
						case "do:restore":
							capture = false;
							newTab = {'windowId':port.tab.windowId, 'url':historyTabRemove.pop()[1], 'selected':true};
							if(nkToolStrip.isOpenToRight)
								newTab.index = port.tab.index + 1;
							chrome.tabs.create(newTab);
							capture = true;
							break;
						case "do:close":
						case "do:righttab":
						case "do:lefttab":
						case "do:lasttab":
						case "do:firsttab":
						case "do:closeall":
						case "do:closeonly":
						case "do:closeleft":
						case "do:closeright":
							chrome.tabs.getAllInWindow(port.tab.windowId, function(tabs)
							{
								var i;
								if (message == "do:close") {
									if(tabs.length == 1)
									chrome.tabs.update(port.tab.id,
									{'url':'chrome://newtab/', 'selected':true});
									else
										chrome.tabs.remove(port.tab.id);
									return;
								}

								if (message.indexOf("do:close")!=-1) {
									i=0;
									var tid;
									while (i < tabs.length) {
										tid=(message == "do:closeright")?
											tabs[tabs.length-1-i++].id:tabs[i++].id;
										if(port.tab.id==tid && message !="do:closeall") {
											if(message == "do:closeleft"
												|| message == "do:closeright"){
													break;
											}
												tid=tabs[i++].id;  //减少循环次数,js优化		
										}
										chrome.tabs.remove(tid);
									}
									return;
								}
								for( i = 0; i < tabs.length; i++)
								{
									if (tabs[i].id === port.tab.id)
									{
										var newtab = null;
										switch(message)
										{
											case "do:righttab":
												newtab = tabs[i+1] || tabs[0];
												break;
											case "do:lefttab":
												newtab = tabs[i-1]
													|| tabs[tabs.length-1];
												break;
											case "do:lasttab":
												newtab = tabs[tabs.length-1];
												break;
											case "do:firsttab":
												newtab = tabs[0];
												break;
										}
										if (newtab)
										{
											chrome.tabs.update(newtab.id,{selected:true});
											chrome.tabs.update(port.tab.id,{selected:false});
										}
									}
								}
							}
						);
						break;
					case "do:config":
						chrome.tabs.create({'windowId':port.tab.windowId, 'index':port.tab.index + 1, 'url':'config.html', 'selected':true});
						break;
					case "do:getcookie":
						nkToolStrip.init();
						break;
					default:
						break;
				}
				/*
				if( message.name == 'MouseEvent' )
				{
					var connection = chrome.tabs.connect(port.tab.id,{name : "nkGesturesTab"});
					connection.postMessage( message );
				}
				*/
			}
		);
//发送用户定义的手势到连接来的tabs
/*
		var connection = chrome.tabs.connect(port.tab.id,{name : "nkGesturesTab"});
		connection.postMessage(nkToolStrip.actionsConfig);
		if ( nkToolStrip.isOpenDrag == true ) {
			connection.postMessage('OpenDrag');
		}
		if ( nkToolStrip.isOpenHint == true ) {
			connection.postMessage('OpenHint');
			if(nkToolStrip.isEnglish)
				connection.postMessage('isEnglish');
		}
		*/
		port.postMessage(nkToolStrip.actionsConfig);
		if ( nkToolStrip.isOpenDrag == true ) {
			port.postMessage('OpenDrag');
		}
		if ( nkToolStrip.isOpenHint == true ) {
			port.postMessage('OpenHint');
			if(nkToolStrip.isEnglish == true )
				port.postMessage('isEnglish');
		}
	}
);

historyTab = new Array();
historyTabRemove = new Array();
capture = true;

chrome.tabs.onCreated.addListener(function(tab) {
	if(!capture)
		return;
	if (!tab.url)
		return;
	//console.log('tabs.onCreated -- window: ' + tab.windowId + ' tab: ' + tab.id + ' index ' + tab.index + ' url ' + tab.url);
	if (historyTab.length >= 10) {
		historyTab.shift();
	}
	historyTab.push(new Array(tab.id,tab.url));
});
chrome.tabs.onUpdated.addListener(function(tabId, props) {
	if(!capture)
		return;
	if(!props.url)
		return;
	var i = 0;
	var isInList = false;
	//console.log('tabs.onUpdated -- tab: ' + tabId + ' status ' + props.status + ' url ' + props.url);
	if (historyTab.length >= 10) {
		historyTab.shift();
	}
	while (historyTab[i]) {

		if (historyTab[i][0] == tabId) {
			historyTab.splice(i,0,new Array(tabId,props.url));
			isInList = true;
			break;
		}
		i++;
	}
	if (!isInList) {
		historyTab.push(new Array(tabId,props.url));
	}
});
chrome.tabs.onRemoved.addListener(function(tabId) {
	var i = 0;

	while (historyTab[i]) {
		deta = historyTab[i];
		if (deta[0] == tabId) {
			if (historyTabRemove.length >= 10) {
				historyTabRemove.shift();
			}
			historyTabRemove.push(deta);
			break;
		}
		i++;
	}
	//console.log('windows.onRemoved -- window: ' + windowId);
});
</script>
